/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.EventDao;
import dao.EventDaoImpl;
import dao.InvitationDao;
import dao.InvitationDaoImpl;
import dao.UserDao;
import dao.UserDaoImpl;
import dao.WeatherDao;
import dao.WeatherDaoImpl;
import entities1.Event;
import entities1.InvitationRequest;
import entities1.Invited;
import entities1.Users;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author takamflore
 */
public class CreateEventServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;
    @EJB
    EventDao eventDao;
    @EJB
    UserDao user;
    @EJB
    WeatherDao weather;
    @EJB
    InvitationDao invitation;        
    String forward="";
    List<Users> listU;
    List<Invited> listI;
    Invited inv;
    InvitationRequest invita;
    
    
    public CreateEventServlet() {
        super();
        eventDao = new EventDaoImpl();
        user= new UserDaoImpl();
        weather= new WeatherDaoImpl();
        invitation= new InvitationDaoImpl();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
         Event event= new Event();
              int n= user.getUserId(request.getRemoteUser());
         String action = request.getParameter("action");

        if (action.equalsIgnoreCase("Create")){
            
            event.setNameEvent(request.getParameter("nameEvent"));
           event.setOrganizer(user.getUser(n));
            //date event
          try {
            Date date = new SimpleDateFormat("MM/dd/yyyy").parse(request.getParameter("date"));
            event.setDateEvent(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
            //hour event
          try {
            Date hour = new SimpleDateFormat("HH:mm:ss").parse(request.getParameter("hour"));
            event.setHour(hour);
        } catch (ParseException e) {
            e.printStackTrace();
        }
            
            event.setAddress(request.getParameter("address"));
            event.setDescription(request.getParameter("description"));
            event.setType("type");
            //
            String weat= request.getParameter("weather");
             event.setWeather(weather.checkWeatherInformationString(weat));
            //
            
            String string= request.getParameter("invited");
            listU=eventDao.splitInvited(string);
            for(Users u:listU){
                event.setInvited(u);
                listI.add(new Invited(u.getId(),event.getId()));            
            }
            event.setInvitedCollection(listI);            
            eventDao.addEvent(event);
            for(Invited i:listI){
                 invita=new InvitationRequest(event.getId(),i.getUser().getId(),false);
            invitation.addInvitation(invita);
            }
            request.setAttribute("events", eventDao.getAllEvent(event.getOrganizer().getId()));
            
        }else{
            request.setAttribute("events", eventDao.getAllEvent(event.getOrganizer().getId()));
        }
        forward ="WEB-INF/entity/event/List_Event.jsp";
         RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
     
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
         request.setAttribute("events", eventDao.getAllEvent(user.getUserId(request.getRemoteUser())));
         forward ="/List_Event.jsp";
         RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
